import { BrowserWindow, ipcMain, screen } from 'electron'

var win = null
const winURL =
  process.env.NODE_ENV === 'development'
    ? `http://localhost:9080/suspension`
    : `file://${__dirname}/suspension/index.html`
ipcMain.on('showSuspensionWindow', (event, args) => {
  console.log('ipcMain:showSuspensionWindow')
  if (win) {
      win.showInactive()
  } else {
    createSuspensionWindow(args)
  }
})

function createSuspensionWindow(args) {
  let mainScreen = screen.getPrimaryDisplay();
  win = new BrowserWindow({
      width: 260,
      height: 580,
      x: mainScreen.bounds.x,
      y: mainScreen.bounds.y,
      type: 'toolbar', //创建的窗口类型为工具栏窗口
      frame: false, //要创建无边框窗口
      resizable: false, //禁止窗口大小缩放
      webPreferences: {
          devTools: false,
          webSecurity: false,
          nodeIntegration: true
      },
      // backgroundColor: '#2e2c29',
      transparent: true, //设置透明
      alwaysOnTop: true, //窗口是否总是显示在其他窗口之前
      movable: true
  })
  const size = mainScreen.workAreaSize //获取显示器的宽高
  const winSize = win.getSize() //获取窗口宽高

  //设置窗口的位置 注意x轴要桌面的宽度 - 窗口的宽度
  win.setPosition(size.width - winSize[0] - 20, size.height - winSize[1] - 100)
  win.loadURL(winURL + "?lessonId=" + args.lessonId + "&userId=" + args.userId);

  //禁止拖拽文件到窗口展示
  win.webContents.on('will-navigate', (event) => event.preventDefault());
  win.on('close', () => {
      win = null
  })
}

function createSuspensionWindow(args) {
    let mainScreen = screen.getPrimaryDisplay();
    win = new BrowserWindow({
        width: 260,
        height: 580,
        x: mainScreen.bounds.x,
        y: mainScreen.bounds.y,
        type: 'toolbar', //创建的窗口类型为工具栏窗口
        frame: false, //要创建无边框窗口
        resizable: false, //禁止窗口大小缩放
        webPreferences: {
            devTools: true,
            webSecurity: false,
            nodeIntegration: true
        },
        // backgroundColor: '#2e2c29',
        transparent: true, //设置透明
        alwaysOnTop: true, //窗口是否总是显示在其他窗口之前
        movable: true
    })
    const size = mainScreen.workAreaSize //获取显示器的宽高
    const winSize = win.getSize() //获取窗口宽高

    //设置窗口的位置 注意x轴要桌面的宽度 - 窗口的宽度
    win.setPosition(size.width - winSize[0] - 20, size.height - winSize[1] - 100)
    win.loadURL(winURL + "?lessonId=" + args.lessonId + "&userId=" + args.userId);

    //禁止拖拽文件到窗口展示
    win.webContents.on('will-navigate', (event) => event.preventDefault());
    // win.webContents.openDevTools()
    win.on('close', () => {
        win = null
    })
}

// 隐藏悬浮窗口
ipcMain.on('hideSuspensionWindow', () => {
  console.log('ipcMain:hideSuspensionWindow')
  if (win) {
    win.close()
  }
})

// 隐藏菜单栏窗口
ipcMain.on('hideTool', () => {
  console.log('ipcMain:hideTool')
  if (win) {
    const winBounds = win.getBounds() // 当前窗口位置大小 {x,y,width,height}
    console.log(JSON.stringify(winBounds));
    win.setPosition(winBounds.x + 78, winBounds.y + 600) 
  }
})

// 显示菜单窗口
ipcMain.on('showTool', () => {
  console.log('ipcMain:showTool')
  if (win) {
    const winBounds = win.getBounds()
    console.log(JSON.stringify(winBounds));
    win.setPosition(winBounds.x - 78, winBounds.y - 600)
  }
})

// 关闭窗口
ipcMain.on('closeWindow', () => {
  if (win) {
    win.close()
  }
})

ipcMain.on('getScreenWH', (event) => {
    let result = screen.getPrimaryDisplay().bounds;
    event.sender.send('getScreenWH-reply', JSON.stringify({width: result.width, height: result.height}))
})
